package com.tingyuge.thinking.spring.configuration.bean.lifecycle;

import com.tingyuge.thinking.spring.ioc.overview.domain.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/7/10
 */
public class  UserHolder implements BeanNameAware, BeanFactoryAware, BeanClassLoaderAware , EnvironmentAware , InitializingBean,
        SmartInitializingSingleton,DisposableBean {
    private final User user;

    private Integer number;

    private String description;

    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return "UserHolder{" +
                "user=" + user +
                ", number=" + number +
                ", beanName=" + beanName +
                ", description='" + description + '\'' +
                '}';
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public User getUser() {
        return user;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public UserHolder(User user) {
        this.user = user;
    }

    private ClassLoader classLoader;
    private BeanFactory factory;
    private String beanName;
    private Environment environment;


    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.factory = beanFactory;
    }

    @Override
    public void setBeanName(String name) {
        this.beanName = name;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    /**
     * 1.依赖注解驱动
     * 当前场景:beanFactory
     */
    @PostConstruct
    public void  initPostConstruct(){
        // postProcessorBeforeInitialization--v3-->v4
        this.description = "the userHolder description is v4";
        System.out.println("initPostConstruct: "+ this.description);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //initPostConstruct v4--->v5
        this.description = "the userHolder description is v5";
        System.out.println("afterPropertiesSet: "+ this.description);

    }
    public void init (){
        //initPostConstruct v5--->v6
        this.description = "the userHolder description is v6";
        System.out.println("自定义:  init: "+ this.description);
    }

    @Override
    public void afterSingletonsInstantiated() {
        //postProcessAfterInitialization v7--->  afterSingletonsInstantiated v8
        this.description = "the userHolder description is v8";
        System.out.println("afterSingletonsInstantiated: "+ this.description);
    }

    @PreDestroy
    public void  preDestroy(){
        //postProcessBeforeDestruction v9--->  afterSingletonsInstantiated 10
        this.description = "the userHolder description is v10";
        System.out.println("preDestroy: "+ this.description);
    }

    @Override
    public void destroy() throws Exception {
        //preDestroy v10--->  destroy v11
        this.description = "the userHolder description is v11";
        System.out.println("destroy: "+ this.description);
    }

    public void doDestroy(){
        //destroy v11--->  doDestroy v12
        this.description = "the doDestroy description is v12";
        System.out.println("doDestroy: "+ this.description);
    }

    @Override
    protected void finalize() throws Throwable {

        System.out.println("userHolder is finalize .....");
    }
}
